package com.duoduo.demo.springnoxml.util;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

public class FacePlusPlusUtils {

	private static String api_key = "jet1JuBaVMm5lNlV4UMZZA91T40AQwBu";
	private static String api_secret = "cS5wT7MVKedoe6Ghw76M58cLFFicrvUX";

	/**
	 * 人脸识别
	 * @param imageUrl
	 */
	public static String detectUrl(String imageUrl) {
		return detect(imageUrl, null, null);
	}

	/**
	 * 人脸识别
	 * @param imageFile
	 */
	public static String detectFile(String imageFile) {
		return detect(null, imageFile, null);
	}

	/**
	 * 人脸识别
	 * @param imageBase64
	 */
	public static String detectBase64(String imageBase64) {
		return detect(null, null, imageBase64);
	}

	/**
	 * 人脸识别
	 * @param imageUrl
	 * @param imageFile
	 * @param imageBase64
	 */
	private static String detect(String imageUrl, String imageFile, String imageBase64) {
		String url = "https://api-cn.faceplusplus.com/facepp/v3/detect";
		Map<String, String> params = new HashMap<String, String>(0);
		params.put("api_key", api_key);
		params.put("api_secret", api_secret);
		if (imageUrl != null) {
			params.put("image_url", imageUrl);
		}
		if (imageFile != null) {
			params.put("image_file", imageFile);
		}
		if (imageBase64 != null) {
			params.put("image_base64", imageBase64);
		}
		try {
			String responseText = HttpClientUtils.doPost(url, params);
			System.out.println("responseText: " + responseText);
			return responseText;
		} catch (IOException e) {
			e.printStackTrace();
			return null;
		}
	}

	/**
	 * 比较图片
	 * @param imageToken1
	 * @param imageToken2
	 */
	public static String compareToken(String imageToken1, String imageToken2) {
		Map<String, String> params = new HashMap<String, String>(0);
		params.put("face_token1", imageToken1);
		params.put("face_token2", imageToken2);
		return compare(params);
	}

	/**
	 * 比较图片
	 * @param imageUrl1
	 * @param imageUrl2
	 */
	public static String compareUrl(String imageUrl1, String imageUrl2) {
		Map<String, String> params = new HashMap<String, String>(0);
		params.put("image_url1", imageUrl1);
		params.put("image_url2", imageUrl2);
		return compare(params);
	}

	/**
	 * 比较图片
	 * @param imageFile1
	 * @param imageFile2
	 */
	public static String compareFile(String imageFile1, String imageFile2) {
		Map<String, String> params = new HashMap<String, String>(0);
		params.put("image_file1", imageFile1);
		params.put("image_file2", imageFile2);
		return compare(params);
	}

	/**
	 * 比较图片
	 * @param imageBase641
	 * @param imageBase642
	 */
	public static String compareBase64(String imageBase641, String imageBase642) {
		Map<String, String> params = new HashMap<String, String>(0);
		params.put("image_base64_1", imageBase641);
		params.put("image_base64_2", imageBase642);
		return compare(params);
	}

	/**
	 * 比较图片
	 * @param imageBase641
	 * @param imageBase642
	 */
	private static String compare(Map<String, String> params) {
		String url = "https://api-cn.faceplusplus.com/facepp/v3/compare";
		params.put("api_key", api_key);
		params.put("api_secret", api_secret);
		try {
			String responseText = HttpClientUtils.doPost(url, params);
			System.out.println("responseText: " + responseText);
			return responseText;
		} catch (IOException e) {
			e.printStackTrace();
			return null;
		}
	}

	/**
	 * 人脸分析
	 * @param faceTokens
	 * @param returnLandmark
	 */
	public static String analyzeLandmark(String faceTokens, String returnLandmark) {
		return analyze(faceTokens, returnLandmark, null);
	}

	/**
	 * 人脸分析
	 * @param faceTokens
	 * @param returnAttributes
	 */
	public static String analyzeAttributes(String faceTokens, String returnAttributes) {
		return analyze(faceTokens, null, returnAttributes);
	}

	/**
	 * 人脸分析
	 * @param faceTokens
	 * @param returnLandmark
	 * @param returnAttributes
	 * @see https://console.faceplusplus.com.cn/documents/4888383
	 */
	public static String analyze(String faceTokens, String returnLandmark, String returnAttributes) {
		String url = "https://api-cn.faceplusplus.com/facepp/v3/face/analyze";
		Map<String, String> params = new HashMap<String, String>(0);
		params.put("api_key", api_key);
		params.put("api_secret", api_secret);
		params.put("face_tokens", faceTokens);
		params.put("return_landmark", (returnLandmark == null ? "0" : returnLandmark));
		params.put("return_attributes", (returnAttributes == null ? "none" : returnAttributes));
		try {
			String responseText = HttpClientUtils.doPost(url, params);
			System.out.println("responseText: " + responseText);
			return responseText;
		} catch (IOException e) {
			e.printStackTrace();
			return null;
		}
	}

	/**
	 * 搜索人脸Token（从指定的人脸集合Token中）
	 * @param imageToken
	 * @param facesetToken
	 */
	public static String searchTokenFromToken(String imageToken, String facesetToken) {
		return searchFromToken(imageToken, null, null, null, facesetToken);
	}

	/**
	 * 搜索人脸Url（从指定的人脸集合Token中）
	 * @param imageUrl
	 * @param facesetToken
	 */
	public static String searchUrlFromToken(String imageUrl, String facesetToken) {
		return searchFromToken(null, imageUrl, null, null, facesetToken);
	}

	/**
	 * 搜索人脸File（从指定的人脸集合Token中）
	 * @param imageFile
	 * @param facesetToken
	 */
	public static String searchFileFromToken(String imageFile, String facesetToken) {
		return searchFromToken(null, null, imageFile, null, facesetToken);
	}

	/**
	 * 搜索人脸Base64（从指定的人脸集合Token中）
	 * @param imageBase64
	 * @param facesetToken
	 */
	public static String searchBase64FromToken(String imageBase64, String facesetToken) {
		return searchFromToken(null, null, null, imageBase64, facesetToken);
	}

	/**
	 * 搜索人脸（从指定的人脸集合Token中）
	 * @param imageToken
	 * @param imageUrl
	 * @param imageFile
	 * @param imageBase64
	 * @param facesetToken
	 */
	private static String searchFromToken(String imageToken, String imageUrl, String imageFile, String imageBase64,
			String facesetToken) {
		String url = "https://api-cn.faceplusplus.com/facepp/v3/search";
		Map<String, String> params = new HashMap<String, String>(0);
		params.put("api_key", api_key);
		params.put("api_secret", api_secret);
		if (imageToken != null) {
			params.put("face_token", imageToken);
		}
		if (imageUrl != null) {
			params.put("image_url", imageUrl);
		}
		if (imageFile != null) {
			params.put("image_file", imageFile);
		}
		if (imageBase64 != null) {
			params.put("image_base64", imageBase64);
		}
		params.put("faceset_token", facesetToken);
		params.put("return_result_count", "1");
		try {
			String responseText = HttpClientUtils.doPost(url, params);
			System.out.println("responseText: " + responseText);
			return responseText;
		} catch (IOException e) {
			e.printStackTrace();
			return null;
		}
	}

	/**
	 * 搜索人脸Token（从用户设置的人脸集合ID中）
	 * @param imageToken
	 * @param outerId
	 */
	public static String searchTokenFromId(String imageToken, String outerId) {
		return searchFromId(imageToken, null, null, null, outerId);
	}

	/**
	 * 搜索人脸Url（从用户设置的人脸集合ID中）
	 * @param imageUrl
	 * @param outerId
	 */
	public static String searchUrlFromId(String imageUrl, String outerId) {
		return searchFromId(null, imageUrl, null, null, outerId);
	}

	/**
	 * 搜索人脸File（从用户设置的人脸集合ID中）
	 * @param imageFile
	 * @param outerId
	 */
	public static String searchFileFromId(String imageFile, String outerId) {
		return searchFromId(null, null, imageFile, null, outerId);
	}

	/**
	 * 搜索人脸Base64（从用户设置的人脸集合ID中）
	 * @param imageBase64
	 * @param outerId
	 */
	public static String searchBase64FromId(String imageBase64, String outerId) {
		return searchFromId(null, null, null, imageBase64, outerId);
	}

	/**
	 * 搜索人脸（从用户设置的人脸集合ID中）
	 * @param imageToken
	 * @param imageUrl
	 * @param imageFile
	 * @param imageBase64
	 * @param outerId
	 */
	private static String searchFromId(String imageToken, String imageUrl, String imageFile, String imageBase64,
			String outerId) {
		String url = "https://api-cn.faceplusplus.com/facepp/v3/search";
		Map<String, String> params = new HashMap<String, String>(0);
		params.put("api_key", api_key);
		params.put("api_secret", api_secret);
		if (imageToken != null) {
			params.put("face_token", imageToken);
		}
		if (imageUrl != null) {
			params.put("image_url", imageUrl);
		}
		if (imageFile != null) {
			params.put("image_file", imageFile);
		}
		if (imageBase64 != null) {
			params.put("image_base64", imageBase64);
		}
		params.put("outer_id", outerId);
		params.put("return_result_count", "1");
		try {
			String responseText = HttpClientUtils.doPost(url, params);
			System.out.println("responseText: " + responseText);
			return responseText;
		} catch (IOException e) {
			e.printStackTrace();
			return null;
		}
	}

	public static void main(String[] args) {
		// FacePlusPlusUtils.detectUrl(
		// "https://bj-mc-prod-asset.oss-cn-beijing.aliyuncs.com/mc-official/images/demo/demo-pic10.jpg");

		FacePlusPlusUtils.compareUrl(
				"https://bj-mc-prod-asset.oss-cn-beijing.aliyuncs.com/mc-official/images/comparing/left_pic_one.jpg",
				"https://bj-mc-prod-asset.oss-cn-beijing.aliyuncs.com/mc-official/images/comparing/right_pic_one.jpg");

		// FacePlusPlusUtils.analyzeLandmark("e6b77a13209fbeb84cd58fea9f89b9a7,51d2c32b8bfb50be5f10655d8c81cb6e", "1");
	}
}
